/**
 * @Author: Hhx06
 * @Email: huangshm@yoozoo.com
 * @Description:
 * @File:  common
 * @Version: 1.0.0
 * @Date: 2022/06/10 16:58
 */

package message

import (
	"github.com/eyotang/boomer"
	"httpTemplate/client"
	"httpTemplate/config"
	"httpTemplate/stats"
)

func Req(ctx boomer.Context, c *client.Client) {
	start := boomer.Now()

	//-------------sdk----------------------
	r, _ := ctx.GetInt("rInt")
	if !DealReq(ctx, c, config.SdkUrl) { //拉取配置
		return
	}
	if r%100 >= config.IsRegisterOrLogin { //注册
		if !RegisterReq(ctx, c, config.SdkUrl) { //失败 上报失败
			stats.RecordFailure("http", "registerTime", boomer.Now()-start, "register error")
			return
		} else { //注册成功登录
			if !LoginReq(ctx, c, config.SdkUrl) { //失败 上报失败
				stats.RecordFailure("http", "loginTime", boomer.Now()-start, "loginTime error")
				return
			}
		}
	} else { //判断大登小登
		if r%100 >= config.IsRegisterOrLogin*config.IsVerify/100 { //5
			if !LoginReq(ctx, c, config.SdkUrl) { //大登失败 上报错误
				stats.RecordFailure("http", "loginTime", boomer.Now()-start, "loginTime error")
				return
			}
		} else { //88
			if !VerifyReq(ctx, c, config.SdkUrl) { //小登失败 上报错误
				stats.RecordFailure("http", "verifyTime", boomer.Now()-start, "verifyTime error")
				return
			}
		}
	}
	//实名制检查
	DealReq(ctx, c, config.SdkUrl)
	//防沉迷
	DealReq(ctx, c, config.FcmUrl)

	//---------------二次登录 ---------------------

	if !DealReq(ctx, c, config.SecondUrl) { //二次登录失败 上报错误
		stats.RecordFailure("http", "clientVerifyTime", boomer.Now()-start, "clientVerifyTime error")
		return
	}

	//--------------角色缓存、区服系统----------------

	if r%100 <= config.IsNoRole { // 无角色缓存  获取角色列表
		flag := true
		if r%100 <= config.IsRoleCache { //无角色信息
			flag = false
		}
		if !DealReq(ctx, c, config.RoleUrl) { //查询游戏角色列表 失败上报
			stats.RecordFailure("http", "QueryGameRoleTime", boomer.Now()-start, "QueryGameRoleTime error")
			return
		}
		if !flag && r%100 > config.IsNoRole*config.IsQueryServer/100 { //无角色缓存需要获取区服列表的
			if !DealReq(ctx, c, config.QueryServerUrl) { //获取区服列表
				stats.RecordFailure("http", "QueryServerListTime", boomer.Now()-start, "QueryServerListTime error")
				return
			}
			if config.IsQueryRecoServerList && !DealReq(ctx, c, config.QueryServerUrl) { //推荐服打开但获取失败
				stats.RecordFailure("http", "QueryRecoServerListTime", boomer.Now()-start, "QueryRecoServerListTime error")
				return
			}
		}
	}
	if !DealReq(ctx, c, config.RoleUrl) { //批量更新游戏角色(一次最多更新100条)
		stats.RecordFailure("http", "QueryRoleTime", boomer.Now()-start, "QueryRoleTime error")
	}
	if !DealReq(ctx, c, config.RoleUrl) {
		stats.RecordFailure("http", "updateTime", boomer.Now()-start, "updateTime error")
	}
	stats.RecordSuccess("http", "fullLinkObFixTime", boomer.Now()-start, 0)

	//------------------充值下单、防沉迷、问卷、礼包码------------------------
	if r%100 > 0 && r%100 <= config.IsOrder { //充值下单、防沉迷
		if !DealReq(ctx, c, config.SecondUrl) { //充值失败 阻断
			stats.RecordFailure("http", "GetOrderIdTime", boomer.Now()-start, "GetOrderIdTime error")
			return
		}
		if !DealReq(ctx, c, config.FcmUrl) {
			stats.RecordFailure("http", "fcmPaymentTime", boomer.Now()-start, "fcmPaymentTime error")
		}
	} else if r%100 > config.IsOrder && r%100 <= config.IsQuestion { //问卷
		if !DealReq(ctx, c, config.QuestionUrl) { //处理用户信息 阻断
			stats.RecordFailure("http", "DealReqTime", boomer.Now()-start, "DealReqTime error")
			return
		}
		if !DealReq(ctx, c, config.QuestionUrl) {
			stats.RecordFailure("http", "QuestionInfoTime", boomer.Now()-start, "QuestionInfoTime error")
		}
		if !DealReq(ctx, c, config.QuestionUrl) {
			stats.RecordFailure("http", "QuestionSubmitTime", boomer.Now()-start, "QuestionSubmitTime error")

		}
	} else if r%100 > config.IsOrder && r%100 <= config.IsCode { //礼包码
		if !DealReq(ctx, c, config.CodeUrl) {
			stats.RecordFailure("http", "CodeTime", boomer.Now()-start, "CodeTime error")

		}
	} else {
		return
	}
	return
}
